package security.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {

	public static final Long TIMEOUT = 300L;
	@Autowired
	private RedisTemplate redisTemplate;

	/**
	 * 批量删除对应的value
	 * 
	 * @param keys
	 */
	public void remove(final String... keys) {
		for (String key : keys) {
			remove(key);
		}
	}

	/**
	 * 批量删除key
	 * 
	 * @param pattern
	 */
	public void removePattern(final String pattern) {
		Set<Serializable> keys = redisTemplate.keys(pattern);
		if (keys.size() > 0)
			redisTemplate.delete(keys);
	}

	/**
	 * 删除对应的value
	 * 
	 * @param key
	 */
	public void remove(final String key) {
		if (exists(key)) {
			redisTemplate.delete(key);
		}
	}

	/**
	 * 判断缓存中是否有对应的value
	 * 
	 * @param key
	 * @return
	 */
	public boolean exists(final String key) {
		return redisTemplate.hasKey(key);
	}

	/**
	 * 读取缓存
	 * 
	 * @param key
	 * @return
	 */
	public Object get(final String key) {
		Object result = null;
		ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
		result = operations.get(key);
		return result;
	}

	/**
	 * 写入缓存
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(final String key, Object value) {
		boolean result = false;
		try {
			ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
			operations.set(key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 写入缓存
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(final String key, Object value, Long expireTime) {
		boolean result = false;
		try {
			ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
			operations.set(key, value);
			redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/** 写入缓存 Hash */
	public boolean setHash(final String key, Map<String, Object> value) {
		boolean result = false;
		try {
			HashOperations<String, String, Object> operations = redisTemplate.opsForHash();
			operations.putAll(key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/** 读取缓存 Hash */
	public Object getHash(final String key, String hkey) {
		Object result = null;
		HashOperations<String, String, Object> operations = redisTemplate.opsForHash();
		result = operations.get(key, hkey);
		return result;
	}
	
	/** 读取全部Hash */
	public Object getHashAll(final String hkey) {
		Object result = null;
		HashOperations<String, String, Object> operations = redisTemplate.opsForHash();
		result = operations.entries(hkey);
		return result;
	}
	
	/** 根据key删除缓存 */
	public boolean remove(final String key, String hkey) {

		try {

			HashOperations<String, String, Object> operations = redisTemplate.opsForHash();
			Long delete = operations.delete(key, hkey);
			if (delete > 0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean delayKeyTime(final String key,Object value, long time){
		ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
		operations.setIfAbsent(key,value);
		//调用这个方法的时候，key是存在的，执行当前方法的时候，判断key是否还存在，不存在设置成功后设置有效期
		redisTemplate.expire(key, time, TimeUnit.SECONDS);
		return true;
	}
}
